#!/bin/bash

##    This file is part of the store/retrieve project
##    (http:##code.google.com/p/storeretrieve/).
##
##    Store/retrieve is free software: you can redistribute it and/or modify
##    it under the terms of the GNU General Public License as published by
##    the Free Software Foundation, either version 3 of the License, or
##    (at your option) any later version.
##
##    Store/retrieve is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##    GNU General Public License for more details.
##
##    You should have received a copy of the GNU General Public License
##    along with store/retrieve. If not, see <http:##www.gnu.org/licenses/>.


VERSION="uploadScript for store/retrieve V. 0.0.1; GPLv3; 2012-06-23"
USAGE="Usage: $( basename $0 ) -u 'https://login:password@server:port/upload.jsp' [-f 'ForeignId'] file1 file2 [...]"
URL="https://login:password@server:port/upload.jsp"
FOREIGNID=""

# Read options
while getopts hvu:f: OPT; do
        case "$OPT" in
        h)
                echo $USAGE
                exit 0;
                ;;
        v)
                echo $VERSION
                exit 0;
                ;;
        u)
                URL="$OPTARG"
                ;;
        f)
                FOREIGNID="$OPTARG"
                ;;
        esac
done
shift $(( $OPTIND - 1 ))

hash curl 2>/dev/null || {
                echo "curl command line tool not found." >&2
                exit -1
}

count="$#"

filelist=""
index=0
for file in "$@"; do
	filelist="$filelist -F file$index=@\"$file\""
	let index=$index+1
done

curl -s -sslv3 -F origin=upload $filelist -F press=Upload "$URL?count=$count" 2>/dev/null | grep '<!-- ID:' | cut -b 10-46
